package com.v5.java.proxy.datasource;

import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.logging.Logger;

import javax.sql.DataSource;

public class MyDataSourcePool implements DataSource {
	
	//初始化该连接池，放入10个链接
	private static String driver = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://127.0.0.1:3306/test";
	private static String user = "root";
	private static String password ="123456";
	
	//定义一个链表代表连接池
	private static LinkedList<Connection> dataSource = new LinkedList<>();
	
	static{
		try{
			//注册数据库的驱动
			Class.forName(driver);
			
			//还有一种方式
			//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			
			//初始化10个链接
			for(int i=0;i<10;i++){
				dataSource.add(DriverManager.getConnection(url, user, password));
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	

	@Override
	public Connection getConnection() throws SQLException {
		// 返回一个连接给客户端
		if(dataSource.size() > 0){
			//存在链接
			//真正的对象
			final Connection conn = dataSource.removeFirst();
			
			//生成真正对象的代理对象，重写close方法，还给连接池
			Connection proxyConn = (Connection) Proxy.newProxyInstance(MyDataSourcePool.class.getClassLoader(), 
					               conn.getClass().getInterfaces(), 
					               new InvocationHandler() {
									
									@Override
									public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
										// 重写close方法
										if(method.getName().equals("close")){
											//调用了close方法 将该链接返回给连接池
											dataSource.add(conn);
											return null;
										}else{
											//调用了其他方法
											return method.invoke(conn, args);
										}
									}
								});
			
			return proxyConn;
		}else{
			//没有链接
			throw new SQLException("系统忙，请稍后.....");
		}
	}
	

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}
